/**
 * Copyright 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.api.adwords.starterkit.wrappers;

import com.google.api.adwords.starterkit.db.keys.NegativeKeywordKey;
import com.google.api.adwords.starterkit.db.structures.NegativeKeywordStructure;
import com.google.api.adwords.starterkit.db.tables.NegativeKeywordTable;
import com.google.api.adwords.starterkit.settings.CampaignSettings;
import com.google.api.adwords.v201101.cm.KeywordMatchType;

import java.sql.Connection;

/**
 * The Class NegativeKeywordWrapper is a wrapper for dealing with Negative
 * Keywords on AdWords.
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class NegativeKeywordWrapper {
  private NegativeKeywordStructure negativeKeywordStructure;
  private NegativeKeywordTable negativeKeywordTable;

  /**
   * Instantiates a new negative keyword wrapper.
   * 
   * @param campaignWrapper the campaign wrapper
   * @param campaignSettings the campaign settings
   * @param conn the conn
   */
  public NegativeKeywordWrapper(final CampaignWrapper campaignWrapper,
      final CampaignSettings campaignSettings, Connection conn) {
    negativeKeywordStructure = new NegativeKeywordStructure();
    negativeKeywordStructure.setCampaignId(campaignWrapper.getCampaignId());
    negativeKeywordStructure.setLocalCampaignId(campaignWrapper.getLocalId());

    negativeKeywordTable = new NegativeKeywordTable(conn);
  }

  /**
   * Instantiates a new negative keyword wrapper.
   * 
   * @param campaignWrapper the campaign wrapper
   * @param text the keyword text
   * @param matchType the keyword match type
   * @param campaignSettings the campaign settings
   * @param conn the DB connection
   */
  public NegativeKeywordWrapper(final CampaignWrapper campaignWrapper,
      final String text, final KeywordMatchType matchType,
      final CampaignSettings campaignSettings, Connection conn) {
    negativeKeywordStructure = new NegativeKeywordStructure();

    negativeKeywordStructure.setText(text);
    negativeKeywordStructure.setMatchType(matchType);
    negativeKeywordStructure.setCampaignId(campaignWrapper.getCampaignId());
    negativeKeywordStructure.setLocalCampaignId(campaignWrapper.getLocalId());
    negativeKeywordStructure.setMatchModifiable(true);

    negativeKeywordTable = new NegativeKeywordTable(conn);

    NegativeKeywordKey negativeKeywordKey =
        new NegativeKeywordKey(negativeKeywordStructure.getLocalCampaignId(),
            negativeKeywordStructure.getMatchType(), 
            negativeKeywordStructure.getText());
    if (negativeKeywordTable.exists(negativeKeywordKey)) {
      synchronize(negativeKeywordKey);
      negativeKeywordTable.setTouched(negativeKeywordKey, true);
    } else {
      negativeKeywordTable.save(negativeKeywordStructure, true, true);
    }
  }

  public String getText() {
    return negativeKeywordStructure.getText();
  }

  public int getLocalId() {
    return negativeKeywordStructure.getLocalId();
  }

  public void setLocalId(int localId) {
    negativeKeywordStructure.setLocalId(localId);
    negativeKeywordTable.save(negativeKeywordStructure, true, true);
  }

  public long getCampaignId() {
    return negativeKeywordStructure.getCampaignId();
  }

  public void setCampaignId(long campaignId) {
    negativeKeywordStructure.setCampaignId(campaignId);
  }

  /**
   * Synchronize NegativeKeyword from DB with NegativeKeyword created by the
   * application.
   * 
   * @param negativeKeywordKey the negative keyword key
   */
  public void synchronize(NegativeKeywordKey negativeKeywordKey) {
    NegativeKeywordStructure negKwdDB =
        negativeKeywordTable.get(negativeKeywordKey);
    negativeKeywordStructure.setLocalCampaignId(negKwdDB.getLocalCampaignId());
    negativeKeywordStructure.setLocalId(negKwdDB.getLocalId());
    negativeKeywordStructure.setCampaignId(negKwdDB.getCampaignId());
    negativeKeywordStructure.setKeywordId(negKwdDB.getKeywordId());
    negativeKeywordStructure.setText(negKwdDB.getText());
    negativeKeywordStructure.setMatchModifiable(negKwdDB.isMatchModifiable());
    negativeKeywordTable.setTouched(negativeKeywordKey, false);
  }
}